package com.be.beadmin.basicData.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.basicData.dto.output.CustomerEx;
import com.be.beadmin.basicData.dto.output.CustomerExcelEx;
import com.be.beadmin.basicData.entity.Customer;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.basicData.service.CustomerService;
import com.be.beadmin.basicData.dto.input.CustomerQueryCriteria;
import com.be.beadmin.basicData.dto.input.CustomerDto;
import com.be.beadmin.logging.annotation.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author chenwei
* @date 2022-12-23
**/
@RestController
public class CustomerController {

    @Autowired
    private  CustomerService customerService;


    @PostMapping(value = "/basicData/getCustomerByPage")
    public ResultModel getCustomerByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(customerService.getCustomerByPage(pageEntity)) ;
    }
    @Log("新建客户")
    @PostMapping("/basicData/addCustomer")
    public ResultModel addCustomer(@RequestBody CustomerDto customerDto){
        customerService.addCustomer(customerDto);
        return ResultModel.success(ResultModel.ADD);
    }


    @PostMapping("/basicData/delCustomer")
    public ResultModel delCustomer(@RequestBody CustomerDto customerDto){
        customerService.delCustomer(customerDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/basicData/updateCustomer")
    public ResultModel updateCustomer(@RequestBody CustomerDto customerDto){
        customerService.updateCustomer(customerDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/basicData/customerExport")
    public void customerExport(HttpServletResponse response , @RequestBody PageEntity pageEntity) throws IOException {
        // 获取条件的分页接口
        Page<CustomerEx> driveParaCalExes = customerService.getCustomerByPage(pageEntity);
        List<CustomerEx> customerExes = driveParaCalExes.getContent();
        List<CustomerExcelEx> customerExcelExes=customerExes.stream().map(item->{
            CustomerExcelEx excel=new CustomerExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("客户接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), CustomerExcelEx.class).sheet("sheet1").doWrite(customerExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), CustomerExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(customerExcelExes);

    }

}